package huzh.foundation.pool;

/**
 *<p>title:LinkedListQueue</p>
 *<p>description:</p>
 * @since 2014-6-7
 * @author huzh
 * @version 1.0
 */
public class LinkedListQueue implements PoolQueue{
    /**first*/
    private ListElement first = null;
    /**last*/
    private ListElement last = null;
    /**element_pool*/
    private ListElementPool element_pool = null;
    /**count*/
    private int count = 0;

    /**
     * @param pool_size
     */
    public LinkedListQueue(int pool_size) {
        this.element_pool = new ListElementPool(pool_size);
    }

    /**
     * 
     */
    public LinkedListQueue() {
        this(100);
    }

    /**
     * @return
     */
    public int size() {
        return this.count;
    }

    /**
     * @param job
     * @return
     */
    public synchronized boolean add(Object job) {
        ListElement el = this.element_pool.getElement();

        if (this.first == null) {
            this.first = el;
            this.last = el;
        } else {
            this.last.setNext(el);
            this.last = el;
        }
        el.setData(job);
        this.count += 1;
        return true;
    }

    /**
     * @return
     */
    public Object removeFirst() {
        ListElement el = this.first;
        Object data = el.getData();
        if (this.first == this.last) {
            this.first = null;
            this.last = null;
        } else {
            this.first = this.first.getNext();
        }
        this.element_pool.returnElement(el);
        this.count -= 1;
        return data;
    }

    /**
     * @return
     */
    public boolean isEmpty() {
        return this.first == null;
    }
}
